package com.fuqi.android.zhongcai.base.view.activity;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.base.Constants;
import com.fuqi.android.zhongcai.base.request.api.GetStaffApi;
import com.fuqi.android.zhongcai.base.request.result.GetStaffResult;
import com.fuqi.android.zhongcai.base.view.adapter.CityListChooseAdapter;
import com.fuqi.android.zhongcai.base.view.adapter.SearchResultAdapter;
import com.fuqi.android.zhongcai.base.view.widget.SideLetterBar;
import com.fuqi.android.zhongcai.base.view.widget.TitleBar;
import com.fuqi.android.zhongcai.business.shuig.request.result.OrderHandleResult;
import com.fuqi.android.zhongcai.sql.Worker;
import com.fuqi.android.zhongcai.sql.dao.SQLDao;
import com.fuqi.android.zhongcai.utils.ActivityUtils;
import com.fuqi.framework.network.code.HandleCode;
import com.fuqi.framework.network.http.RequestManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class ChooseWorkerActivity extends BaseActivity implements View.OnClickListener{


    @Bind(R.id.et_search)
    EditText etSearch;
    @Bind(R.id.iv_search_clear)
    ImageView ivSearchClear;
    @Bind(R.id.listview_all_city)
    ListView listviewAllCity;
    @Bind(R.id.tv_letter_overlay)
    TextView tvLetterOverlay;
    @Bind(R.id.side_letter_bar)
    SideLetterBar sideLetterBar;
    @Bind(R.id.listview_search_result)
    ListView listviewSearchResult;
    @Bind(R.id.empty_view)
    LinearLayout emptyView;
    private CityListChooseAdapter mCityChooseAdapter;
    private SearchResultAdapter mSearchResultAdapter;
    private  List<Worker> mAllWorkers;

    @OnClick({R.id.btn_submit})
    public void onViewClick(View view){

        switch (view.getId()){
            case R.id.btn_submit:
                ArrayList<Worker> chooseWorkers = mCityChooseAdapter.getmWorkers();
                if(chooseWorkers == null || chooseWorkers.size() == 0) {
                    ActivityUtils.toast("请选择最少一个工人。");
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("workers",chooseWorkers);
                setResult(RESULT_OK,intent);
                finish();
                break;
        }

    }

    @Override
    public void initView() {

        titleBar.setTitle("工人管理");

        sideLetterBar.setOverlay(tvLetterOverlay);
        sideLetterBar.setOnLetterChangedListener(new SideLetterBar.OnLetterChangedListener() {
            @Override
            public void onLetterChanged(String letter) {
                int position = mCityChooseAdapter.getLetterPosition(letter);
                listviewAllCity.setSelection(position);
            }
        });

        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String keyword = s.toString();
                if (TextUtils.isEmpty(keyword)) {
                    ivSearchClear.setVisibility(View.GONE);
                    emptyView.setVisibility(View.GONE);
                    listviewSearchResult.setVisibility(View.GONE);
                } else {
                    ivSearchClear.setVisibility(View.VISIBLE);
                    listviewSearchResult.setVisibility(View.VISIBLE);
                    List<Worker> result = SQLDao.searchWorkerByKeyword(keyword);
                    if (result == null || result.size() == 0) {
                        emptyView.setVisibility(View.VISIBLE);
                    } else {
                        emptyView.setVisibility(View.GONE);
                        mSearchResultAdapter.changeData(result);
                    }
                }
            }
        });

        listviewSearchResult.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ActivityUtils.toast(mSearchResultAdapter.getItem(position).getName());
            }
        });

        ivSearchClear.setOnClickListener(this);
    }

    @SuppressLint("RtlHardcoded")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_search_clear:
                etSearch.setText("");
                ivSearchClear.setVisibility(View.GONE);
                emptyView.setVisibility(View.GONE);
                listviewSearchResult.setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }



    @Override
    public void initListener() {
        mCityChooseAdapter.setOnCityClickListener(new CityListChooseAdapter.OnCityClickListener() {
            @Override
            public void onItemClick(Worker worker) {

//                ActivityUtils.toast(worker.getName());

            }

        });
        titleBar.addAction(new TitleBar.ImageAction(R.mipmap.btn_add) {
            @Override
            public void performAction(View view) {

                ActivityUtils.startActivity(ChooseWorkerActivity.this, AddWorkerActivity.class);
            }
        });

    }

    @Override
    public void initData() {

//        SQLDao.getInstance(ChooseWorkerActivity.this).clearWorker();
        mAllWorkers = SQLDao.getInstance(ChooseWorkerActivity.this).getAllWorker();
        if(mAllWorkers == null || mAllWorkers.size() == 0) {
            mAllWorkers = new ArrayList<>();
            RequestManager.getInstance().call(new GetStaffApi(new GetStaffApi.GetStaffParams(Constants.ROLE_SGGR), respWorkersListener, errorListener));
        }
        mCityChooseAdapter = new CityListChooseAdapter(ChooseWorkerActivity.this, mAllWorkers);

        mSearchResultAdapter = new SearchResultAdapter(ChooseWorkerActivity.this, null);
        listviewAllCity.setAdapter(mCityChooseAdapter);
        listviewSearchResult.setAdapter(mSearchResultAdapter);


}

    Response.Listener<GetStaffResult[]> respWorkersListener = new Response.Listener<GetStaffResult[]>() {

        @Override
        public void onResponse(GetStaffResult[] response) {

            if (HandleCode.requestSuccess()) {

                if (response != null && response.length > 0){

                    mCityChooseAdapter.loadMoreData(response);
                }


                Log.i("juner", "juner respListener response : ");
            } else {



            }
        }
    };


    Response.Listener<OrderHandleResult> respJCYListener = new Response.Listener<OrderHandleResult>() {

        @Override
        public void onResponse(OrderHandleResult response) {

            if (HandleCode.requestSuccess()) {


                Log.i("juner", "juner respListener response : ");
            } else {



            }
        }
    };


    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

            Log.i("juner", "juner  error.getMessage(): " + error.getMessage());

        }
    };

    @Override
    public boolean hasTitleBar() {
        return true;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_choose_worker;
    }
}
